# -*- coding: utf-8 -*-
'''
Created on 2012-2-16

@author: Labelvic
'''
from pyxmpp.all import JID,Iq

class iq_sit_down:
    def __init__(self, myclient, scene, desk=None, nearby=None):
        """
        parameter:
        scene     'integer'
        desk      'integer'               
        nearby    'integer' 0 or 1        
        """
        self.client = myclient
        self.scene = scene
        self.desk = desk
        self.nearby = nearby
    
    def send_iq(self):
        iq = Iq(to_jid=JID("scene."+self.client.jid.domain), stanza_type="set")
        q = iq.new_query("jabber:moblove:scene:sitdown")        
        appoint = q.newChild(q.ns(),"appoint",None)
        if (self.scene>=0 and self.scene<=3):
            appoint.newProp("scene", str(self.scene))
        if (self.desk>=0):
            appoint.newProp("desk", str(self.desk))
        if (self.nearby>=0):
            appoint.newProp("nearby", str(self.nearby))
        self.client.stream.set_response_handlers(iq,
                                          self.__sitscene_result,self.__sitscene_error)
        self.client.stream.send(iq)
        
    def __sitscene_result(self, iq):
        sceneinfo=iq.xpath_eval("g:query/g:sceneinfo",{"g":"jabber:moblove:scene:sitdown"})
        for scene in sceneinfo:
            sceneid = scene.prop("scene")
            break
        deskinfo=iq.xpath_eval("g:query/g:deskinfo",{"g":"jabber:moblove:scene:sitdown"})
        for desk in deskinfo:
            deskid = desk.prop("desk")
            sessionid = desk.prop("sessionid")
            break
        userinfo=iq.xpath_eval("g:query/g:user",{"g":"jabber:moblove:scene:sitdown"})
        for user in userinfo:
            useritem={"jid":user.prop("jid"), "vip":user.prop("vip"),
                 "sex":user.prop("sex"), "nick":user.prop("nick"),"popularity":user.prop("popularity"),
                 "distance":user.prop("distance"), "scene":sceneid, "desk":deskid}
            break     
        #if (self.client.chat_ui):                               
            #self.client.chat_ui.show(self.client, sceneid, deskid, sessionid, useritem)
        if (self.client.logic_ui):
            self.client.logic_ui.add_user(useritem,self.client)
    
    def __sitscene_error(self, iq):
        _errornode=iq.xpath_eval("g:error",{"g":"jabber:moblove:scene:sitdown"})

class iq_get_scene_users:
    def __init__(self, myclient):
        self.client = myclient
    def send_iq(self):        
        iq = Iq(to_jid=JID("scene."+self.client.jid.domain), stanza_type="get")
        q = iq.new_query("jabber:moblove:scene:userlist")
        index = q.newChild(q.ns(), "index", None)
        index.newProp("start", "0")
        index.newProp("end", "1000")
        q.newChild(q.ns(), "refresh", None)        
        self.client.stream.set_response_handlers(iq,
                                          self.__userlist_result,self.__userlist_error)
        self.client.stream.send(iq)        
        
    def __userlist_result(self, iq):
        self.client.logic_ui.clear_user(self.client)
        userlists=iq.xpath_eval("g:query/g:user",{"g":"jabber:moblove:scene:userlist"})        
        for user in userlists:
            try:
                useritem={"jid":user.prop("jid"), "desk":user.prop("desk"), "vip":user.prop("vip"),
                 "sex":user.prop("sex"), "nick":user.prop("nick"), "busy":user.prop("busy"),
                 "undisturbed":user.prop("undisturbed"), "popularity":user.prop("popularity")}
#                if (self.sceneid!=None):
#                    useritem['scene'] = str(self.sceneid)
                self.client.logic_ui.add_user(useritem, self.client)                
            except:
                pass
    
    def __userlist_error(self, iq):
        pass
    
class iq_get_all_scene_users:
    """
            依次坐到0~3号场景 ，并取用户列表
    """
    def __init__(self, myclient):
        self.client = myclient
        self.currentscene = 0
    
    def send_iq(self):
        self.__send_iq(0)
        
    def __send_iq(self, index):
        if (index>3):
            return
        self.currentscene = index
        iq = Iq(to_jid=JID("scene."+self.client.jid.domain), stanza_type="set")
        q = iq.new_query("jabber:moblove:scene:sitdown")        
        appoint = q.newChild(q.ns(),"appoint",None)
        appoint.newProp("scene", str(self.currentscene))
        self.client.stream.set_response_handlers(iq,
                                          self.__sitscene_result,self.__sitscene_error)
        self.client.stream.send(iq)        
        
    def __sitscene_result(self, iq):
        sceneinfo=iq.xpath_eval("g:query/g:sceneinfo",{"g":"jabber:moblove:scene:sitdown"})
        _deskinfo=iq.xpath_eval("g:query/g:deskinfo",{"g":"jabber:moblove:scene:sitdown"})
        _userinfo=iq.xpath_eval("g:query/g:user",{"g":"jabber:moblove:scene:sitdown"})
        for scene in sceneinfo:
            self.sceneid = int(scene.prop("scene"))
            break 
        if (self.currentscene<4):
            self.get_scene_user_list(True)
    
    def __sitscene_error(self, iq):
        _errornode=iq.xpath_eval("g:error",{"g":"jabber:moblove:scene:sitdown"})
        
    
    def get_scene_user_list(self, refresh):                
        iq = Iq(to_jid=JID("scene."+self.client.jid.domain), stanza_type="get")
        q = iq.new_query("jabber:moblove:scene:userlist")
        index = q.newChild(q.ns(), "index", None)
        index.newProp("start", "0")
        index.newProp("end", "100")
        if (refresh):
            q.newChild(q.ns(), "refresh", None)        
        self.client.stream.set_response_handlers(iq,
                                          self.__userlist_result,self.__userlist_error)
        self.client.stream.send(iq)
        self.__send_iq(self.currentscene+1)
            
        
    def __userlist_result(self, iq):
        userlists=iq.xpath_eval("g:query/g:user",{"g":"jabber:moblove:scene:userlist"})        
        for user in userlists:
            try:
                useritem={"jid":user.prop("jid"), "desk":user.prop("desk"), "vip":user.prop("vip"),
                 "sex":user.prop("sex"), "nick":user.prop("nick"), "busy":user.prop("busy"),
                 "undisturbed":user.prop("undisturbed"), "popularity":user.prop("popularity")}
                if (self.sceneid!=None):
                    useritem['scene'] = str(self.sceneid)
                self.client.logic_ui.add_user(useritem, self.client)
            except:
                pass
    
    def __userlist_error(self, iq):
        pass
    